home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / tar-mode.el.z / tar-mode.el
Encoding:
Text File  |  1998-05-21  |  52.3 KB  |  1,382 lines

  1. ;;; tar-mode.el --- simple editing of tar files from GNU emacs
  2.  
  3. ;; Copyright (C) 1990-1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Jamie Zawinski <jwz@netscape.com>
  6. ;; Keywords: unix
  7. ;; Created: 4 Apr 1990
  8. ;; Version: 1.31, 15 Dec 93
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  24. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  25. ;; Boston, MA 02111-1307, USA.
  26.  
  27. ;;; Synched up with: Not synched with FSF.
  28.  
  29. ;;; Commentary:
  30.  
  31. ;; This package attempts to make dealing with Unix 'tar' archives easier.
  32. ;; When this code is loaded, visiting a file whose name ends in '.tar' will
  33. ;; cause the contents of that archive file to be displayed in a Dired-like
  34. ;; listing.  It is then possible to use the customary Dired keybindings to
  35. ;; extract sub-files from that archive, either by reading them into their own
  36. ;; editor buffers, or by copying them directly to arbitrary files on disk.
  37. ;; It is also possible to delete sub-files from within the tar file and write
  38. ;; the modified archive back to disk, or to edit sub-files within the archive
  39. ;; and re-insert the modified files into the archive.  See the documentation
  40. ;; string of tar-mode for more info.
  41.  
  42. ;; To autoload, add this to your .emacs file:
  43. ;;
  44. ;;  (setq auto-mode-alist (cons '("\\.tar$" . tar-mode) auto-mode-alist))
  45. ;;  (autoload 'tar-mode "tar-mode")
  46. ;;
  47. ;; But beware: for certain tar files - those whose very first file has 
  48. ;; a -*- property line - autoloading won't work.  See the function 
  49. ;; "tar-normal-mode" to understand why.
  50.  
  51. ;; This code now understands the extra fields that GNU tar adds to tar files.
  52.  
  53. ;; This interacts correctly with "uncompress.el" in the Emacs library,
  54. ;; and with sufficiently recent versions of "crypt.el" by Kyle Jones.
  55.  
  56. ;;    ***************   TO DO   *************** 
  57. ;;
  58. ;; o  There should be a command to extract the whole current buffer into files
  59. ;;    on disk (right now you have to do the subfiles one at a time.)
  60. ;;
  61. ;; o  chmod should understand "a+x,og-w".
  62. ;;
  63. ;; o  It's not possible to add a NEW file to a tar archive; not that 
  64. ;;    important, but still...
  65. ;;
  66. ;; o  The code is less efficient that it could be - in a lot of places, I
  67. ;;    pull a 512-character string out of the buffer and parse it, when I could
  68. ;;    be parsing it in place, not garbaging a string.  Should redo that.
  69. ;;
  70. ;; o  I'd like a command that searches for a string/regexp in every subfile
  71. ;;    of an archive, where <esc> would leave you in a subfile-edit buffer.
  72. ;;    (Like M-s in VM and M-r in the Zmacs mail reader.)
  73. ;;
  74. ;; o  Sometimes (but not always) reverting the tar-file buffer does not 
  75. ;;    re-grind the listing, and you are staring at the binary tar data.
  76. ;;    Typing 'g' again immediately after that will always revert and re-grind
  77. ;;    it, though.  I have no idea why this happens.
  78. ;;
  79. ;; o  If you edit a subfile, and selective-display gets set to t, then when
  80. ;;    we save the subfile, we should map ^M -> ^J.
  81. ;;
  82. ;; o  Block files, sparse files, continuation files, and the various header
  83. ;;    types aren't editable.  Actually I don't know that they work at all.
  84. ;;    If you know that they work, or know that they don't, please let me know.
  85. ;;
  86. ;; o  Tar files inside of tar files don't work.
  87. ;;
  88. ;; o  When using crypt-mode, you can't save a compressed or encrypted subfile
  89. ;;    of a tar file back into the tar file: it is saved uncompressed.
  90.  
  91. ;;; Code:
  92.  
  93. (defgroup tar ()
  94.   "Simple editing of tar files from GNU emacs."
  95.   :group 'unix
  96.   :group 'data)
  97.  
  98.  
  99. (defcustom tar-anal-blocksize 20
  100.   "*The blocksize of tar files written by Emacs, or nil, meaning don't care.
  101. The blocksize of a tar file is not really the size of the blocks; rather, it is
  102. the number of blocks written with one system call.  When tarring to a tape, 
  103. this is the size of the *tape* blocks, but when writing to a file, it doesn't
  104. matter much.  The only noticeable difference is that if a tar file does not
  105. have a blocksize of 20, the tar program will issue a warning; all this really
  106. controls is how many null padding bytes go on the end of the tar file."
  107.   :type 'integer
  108.   :group 'tar)
  109.  
  110. (defcustom tar-update-datestamp t
  111.   "*Whether tar-mode should play fast and loose with sub-file datestamps;
  112. if this is true, then editing and saving a tar file entry back into its
  113. tar file will update its datestamp.  If false, the datestamp is unchanged.
  114. You may or may not want this - it is good in that you can tell when a file
  115. in a tar archive has been changed, but it is bad for the same reason that
  116. editing a file in the tar archive at all is bad - the changed version of 
  117. the file never exists on disk.
  118.  
  119. This does not work in Emacs 18, because there's no way to get the current 
  120. time as an integer - if this var is true, then editing a file sets its date
  121. to midnight, Jan 1 1970 GMT, which happens to be what 0 encodes."
  122.   :type 'boolean
  123.   :group 'tar)
  124.  
  125.  
  126. ;;; First, duplicate some Common Lisp functions; I used to just (require 'cl)
  127. ;;; but "cl.el" was messing some people up (also it's really big).
  128.  
  129. ;; No need for that stuff anymore -- XEmacs preloads cl.el anyway.
  130.  
  131.  
  132. ;;; down to business.
  133.  
  134. (defmacro make-tar-header (name mode uid git size date ck lt ln
  135.                 magic uname gname devmaj devmin)
  136.   (list 'vector name mode uid git size date ck lt ln
  137.     magic uname gname devmaj devmin))
  138.  
  139. (defmacro tar-header-name (x) (list 'aref x 0))
  140. (defmacro tar-header-mode (x) (list 'aref x 1))
  141. (defmacro tar-header-uid  (x) (list 'aref x 2))
  142. (defmacro tar-header-gid  (x) (list 'aref x 3))
  143. (defmacro tar-header-size (x) (list 'aref x 4))
  144. (defmacro tar-header-date (x) (list 'aref x 5))
  145. (defmacro tar-header-checksum  (x) (list 'aref x 6))
  146. (defmacro tar-header-link-type (x) (list 'aref x 7))
  147. (defmacro tar-header-link-name (x) (list 'aref x 8))
  148. (defmacro tar-header-magic (x) (list 'aref x 9))
  149. (defmacro tar-header-uname (x) (list 'aref x 10))
  150. (defmacro tar-header-gname (x) (list 'aref x 11))
  151. (defmacro tar-header-dmaj (x) (list 'aref x 12))
  152. (defmacro tar-header-dmin (x) (list 'aref x 13))
  153.  
  154. (defmacro make-tar-desc (data-start tokens)
  155.   (list 'cons data-start tokens))
  156.  
  157. (defmacro tar-desc-data-start (x) (list 'car x))
  158. (defmacro tar-desc-tokens     (x) (list 'cdr x))
  159.  
  160. (defconst tar-name-offset 0)
  161. (defconst tar-mode-offset (+ tar-name-offset 100))
  162. (defconst tar-uid-offset  (+ tar-mode-offset 8))
  163. (defconst tar-gid-offset  (+ tar-uid-offset 8))
  164. (defconst tar-size-offset (+ tar-gid-offset 8))
  165. (defconst tar-time-offset (+ tar-size-offset 12))
  166. (defconst tar-chk-offset  (+ tar-time-offset 12))
  167. (defconst tar-linkp-offset (+ tar-chk-offset 8))
  168. (defconst tar-link-offset (+ tar-linkp-offset 1))
  169. ;;; GNU-tar specific slots.
  170. (defconst tar-magic-offset (+ tar-link-offset 100))
  171. (defconst tar-uname-offset (+ tar-magic-offset 8))
  172. (defconst tar-gname-offset (+ tar-uname-offset 32))
  173. (defconst tar-dmaj-offset (+ tar-gname-offset 32))
  174. (defconst tar-dmin-offset (+ tar-dmaj-offset 8))
  175. (defconst tar-end-offset (+ tar-dmin-offset 8))
  176.  
  177. (defun tokenize-tar-header-block (string)
  178.   "Returns a 'tar-header' structure (a list of name, mode, uid, gid, size, 
  179. write-date, checksum, link-type, and link-name)."
  180.   (cond ((< (length string) 512) nil)
  181.     (                ;(some 'plusp string)         ; <-- oops, massive cycle hog!
  182.      (or (not (= 0 (aref string 0))) ; This will do.
  183.          (not (= 0 (aref string 101))))
  184.      (let* ((name-end (1- tar-mode-offset))
  185.         (link-end (1- tar-magic-offset))
  186.         (uname-end (1- tar-gname-offset))
  187.         (gname-end (1- tar-dmaj-offset))
  188.         (link-p (aref string tar-linkp-offset))
  189.         (magic-str (substring string tar-magic-offset (1- tar-uname-offset)))
  190.         (uname-valid-p (or (string= "ustar  " magic-str) (string= "GNUtar " magic-str)))
  191.         name
  192.         (nulsexp   "[^\000]*\000"))
  193.        (and (string-match nulsexp string tar-name-offset) (setq name-end (min name-end (1- (match-end 0)))))
  194.        (and (string-match nulsexp string tar-link-offset) (setq link-end (min link-end (1- (match-end 0)))))
  195.        (and (string-match nulsexp string tar-uname-offset) (setq uname-end (min uname-end (1- (match-end 0)))))
  196.        (and (string-match nulsexp string tar-gname-offset) (setq gname-end (min gname-end (1- (match-end 0)))))
  197.        (setq name (substring string tar-name-offset name-end)
  198.          link-p (if (or (= link-p 0) (= link-p ?0))
  199.                 nil
  200.               (- link-p ?0)))
  201.        (if (and (null link-p) (string-match "/$" name)) (setq link-p 5)) ; directory
  202.        (make-tar-header
  203.         name
  204.         (tar-parse-octal-integer string tar-mode-offset (1- tar-uid-offset))
  205.         (tar-parse-octal-integer string tar-uid-offset (1- tar-gid-offset))
  206.         (tar-parse-octal-integer string tar-gid-offset (1- tar-size-offset))
  207.         (tar-parse-octal-integer string tar-size-offset (1- tar-time-offset))
  208.         (tar-parse-octal-integer-32 string tar-time-offset (1- tar-chk-offset))
  209.         (tar-parse-octal-integer string tar-chk-offset (1- tar-linkp-offset))
  210.         link-p
  211.         (substring string tar-link-offset link-end)
  212.         uname-valid-p
  213.         (and uname-valid-p (substring string tar-uname-offset uname-end))
  214.         (and uname-valid-p (substring string tar-gname-offset gname-end))
  215.         (tar-parse-octal-integer string tar-dmaj-offset (1- tar-dmin-offset))
  216.         (tar-parse-octal-integer string tar-dmin-offset (1- tar-end-offset))
  217.         )))
  218.     (t 'empty-tar-block)))
  219.  
  220.  
  221. (defun tar-parse-octal-integer (string &optional start end)
  222.   "deletes all your files, and then reboots."
  223.   (if (null start) (setq start 0))
  224.   (if (null end) (setq end (length string)))
  225.   (if (= (aref string start) 0)
  226.       0
  227.     (let ((n 0))
  228.       (while (< start end)
  229.     (setq n (if (< (aref string start) ?0) n
  230.           (+ (* n 8) (- (aref string start) 48)))
  231.           start (1+ start)))
  232.       n)))
  233.  
  234. (defun tar-parse-octal-integer-32 (string &optional start end)
  235.   ;; like tar-parse-octal-integer, but returns a cons of two 16-bit numbers,
  236.   ;; since elisp can't handle integers of that magnitude.
  237.   (or start (setq start 0))
  238.   (or end (setq end (length string)))
  239.   (let ((top (tar-parse-octal-integer string start (- end 6)))
  240.     (bot (tar-parse-octal-integer string (- end 6) end)))
  241.     (setq top (logior (ash top 2) (ash bot -16)))
  242.     (setq bot (logand bot 65535))
  243.     (cons top bot)))
  244.  
  245. (defun tar-parse-octal-integer-safe (string)
  246.   (let ((L (length string)))
  247.     (if (= L 0) (error "empty string"))
  248.     (dotimes (i L)
  249.       (if (or (< (aref string i) ?0)
  250.           (> (aref string i) ?7))
  251.       (error "'%c' is not an octal digit."))))
  252.   (tar-parse-octal-integer string))
  253.  
  254.  
  255. (defun checksum-tar-header-block (string)
  256.   "Computes and returns a tar-acceptable checksum for this block."
  257.   (let* ((chk-field-start tar-chk-offset)
  258.      (chk-field-end (+ chk-field-start 8))
  259.      (sum 0)
  260.      (i 0))
  261.     ;; Add up all of the characters except the ones in the checksum field.
  262.     ;; Add that field as if it were filled with spaces.
  263.     (while (< i chk-field-start)
  264.       (setq sum (+ sum (aref string i))
  265.         i (1+ i)))
  266.     (setq i chk-field-end)
  267.     (while (< i 512)
  268.       (setq sum (+ sum (aref string i))
  269.         i (1+ i)))
  270.     (+ sum (* 32 8))))
  271.  
  272. (defun check-tar-header-block-checksum (hblock desired-checksum file-name)
  273.   "Beep and print a warning if the checksum doesn't match."
  274.   (if (not (= desired-checksum (checksum-tar-header-block hblock)))
  275.       (progn (beep) (message "Invalid checksum for file %s!" file-name))))
  276.  
  277. (defun recompute-tar-header-block-checksum (hblock)
  278.   "Modifies the given string to have a valid checksum field."
  279.   (let* ((chk (checksum-tar-header-block hblock))
  280.      (chk-string (format "%6o" chk))
  281.      (l (length chk-string)))
  282.     (aset hblock 154 0)
  283.     (aset hblock 155 32)
  284.     (dotimes (i l) (aset hblock (- 153 i) (aref chk-string (- l i 1)))))
  285.   hblock)
  286.  
  287.  
  288. (defun tar-grind-file-mode (mode string start)
  289.   "Write a \"-rw--r--r-\" representing MODE into STRING beginning at START."
  290.   (aset string start       (if (zerop (logand 256 mode)) ?- ?r))
  291.   (aset string (+ start 1) (if (zerop (logand 128 mode)) ?- ?w))
  292.   (aset string (+ start 2) (if (zerop (logand  64 mode)) ?- ?x)) 
  293.   (aset string (+ start 3) (if (zerop (logand  32 mode)) ?- ?r))
  294.   (aset string (+ start 4) (if (zerop (logand  16 mode)) ?- ?w))
  295.   (aset string (+ start 5) (if (zerop (logand   8 mode)) ?- ?x))
  296.   (aset string (+ start 6) (if (zerop (logand   4 mode)) ?- ?r))
  297.   (aset string (+ start 7) (if (zerop (logand   2 mode)) ?- ?w))
  298.   (aset string (+ start 8) (if (zerop (logand   1 mode)) ?- ?x))
  299.   (if (zerop (logand 1024 mode)) nil (aset string (+ start 2) ?s))
  300.   (if (zerop (logand 2048 mode)) nil (aset string (+ start 5) ?s))
  301.   string)
  302.  
  303.  
  304. (defconst tar-can-print-dates (or (fboundp 'current-time)
  305.                   (fboundp 'current-time-seconds))
  306.   "true if this emacs has been built with time-printing support")
  307.  
  308. (defun summarize-tar-header-block (tar-hblock &optional mod-p)
  309.   "Returns a line similar to the output of 'tar -vtf'."
  310.   (let ((name (tar-header-name tar-hblock))
  311.     (mode (tar-header-mode tar-hblock))
  312.     (uid (tar-header-uid tar-hblock))
  313.     (gid (tar-header-gid tar-hblock))
  314.     (uname (tar-header-uname tar-hblock))
  315.     (gname (tar-header-gname tar-hblock))
  316.     (size (tar-header-size tar-hblock))
  317.     (time (tar-header-date tar-hblock))
  318.     (ck (tar-header-checksum tar-hblock))
  319.     (link-p (tar-header-link-type tar-hblock))
  320.     (link-name (tar-header-link-name tar-hblock))
  321.     )
  322.     (let* ((left 11)
  323.        (namew 8)
  324.        (groupw 8)
  325.        (sizew 8)
  326.        (datew (if tar-can-print-dates 15 2))
  327.        (slash (1- (+ left namew)))
  328.        (lastdigit (+ slash groupw sizew))
  329.        (namestart (+ lastdigit datew))
  330.        (string (make-string (+ namestart (length name) (if link-p (+ 5 (length link-name)) 0)) 32))
  331.        (type (tar-header-link-type tar-hblock)))
  332.       (aset string 0 (if mod-p ?* ? ))
  333.       (aset string 1
  334.         (cond ((or (eq type nil) (eq type 0)) ?-)
  335.           ((eq type 1) ?l)    ; link
  336.           ((eq type 2) ?s)    ; symlink
  337.           ((eq type 3) ?c)    ; char special
  338.           ((eq type 4) ?b)    ; block special
  339.           ((eq type 5) ?d)    ; directory
  340.           ((eq type 6) ?p)    ; FIFO/pipe
  341.           ((eq type 20) ?*)    ; directory listing
  342.           ((eq type 29) ?M)    ; multivolume continuation
  343.           ((eq type 35) ?S)    ; sparse
  344.           ((eq type 38) ?V)    ; volume header
  345.           ))
  346.       (tar-grind-file-mode mode string 2)
  347.       (setq uid (if (= 0 (length uname)) (int-to-string uid) uname))
  348.       (setq gid (if (= 0 (length gname)) (int-to-string gid) gname))
  349.       (setq size (int-to-string size))
  350.       (dotimes (i (min (1- namew) (length uid))) (aset string (- slash i) (aref uid (- (length uid) i 1))))
  351.       (aset string (1+ slash) ?/)
  352.       (dotimes (i (min (1- groupw) (length gid))) (aset string (+ (+ slash 2) i) (aref gid i)))
  353.       (dotimes (i (min sizew (length size))) (aset string (- lastdigit i) (aref size (- (length size) i 1))))
  354.  
  355.       (if tar-can-print-dates
  356.       (let* ((year (substring (current-time-string) -4))
  357.          ;; in v18, current-time-string doesn't take an argument
  358.          (file (current-time-string time))
  359.          (file-year (substring file -4))
  360.          (str (if (equal year file-year)
  361.               (substring file 4 16)
  362.             (concat (substring file 4 11) " " file-year))))
  363.         (dotimes (i 12) (aset string (- namestart (- 13 i)) (aref str i)))))
  364.  
  365.       (dotimes (i (length name)) (aset string (+ namestart i) (aref name i)))
  366.       (if (or (eq link-p 1) (eq link-p 2))
  367.       (progn
  368.         (dotimes (i 3) (aset string (+ namestart 1 (length name) i) (aref (if (= link-p 1) "==>" "-->") i)))
  369.         (dotimes (i (length link-name)) (aset string (+ namestart 5 (length name) i) (aref link-name i)))))
  370.       string)))
  371.  
  372.  
  373. ;; buffer-local variables in the tar file's buffer:
  374. ;;
  375. (defvar tar-parse-info)            ; the header structures
  376. (defvar tar-header-offset)        ; the end of the "pretty" data
  377.  
  378. (defun tar-summarize-buffer ()
  379.   "Parse the contents of the tar file in the current buffer, and place a
  380. dired-like listing on the front; then narrow to it, so that only that listing
  381. is visible (and the real data of the buffer is hidden)."
  382.   (message "parsing tar file...")
  383.   (let* ((result '())
  384.      (pos 1)
  385.      (bs (max 1 (- (buffer-size) 1024))) ; always 2+ empty blocks at end.
  386.      (bs100 (max 1 (/ bs 100)))
  387.      (tokens nil))
  388.     (while (not (eq tokens 'empty-tar-block))
  389.       (if (> (+ pos 512) (point-max))
  390.       (error "truncated tar file"))
  391.       (let* ((hblock (buffer-substring pos (+ pos 512))))
  392.     (setq tokens (tokenize-tar-header-block hblock))
  393.     (setq pos (+ pos 512))
  394.     (message "parsing tar file...%s%%"
  395.                 ;(/ (* pos 100) bs)   ; this gets round-off lossage
  396.          (/ pos bs100)        ; this doesn't
  397.          )
  398.     (if (eq tokens 'empty-tar-block)
  399.         nil
  400.       (if (null tokens) (error "premature EOF parsing tar file."))
  401.       (if (eq (tar-header-link-type tokens) 20)
  402.           ;; Foo.  There's an extra empty block after these.
  403.           (setq pos (+ pos 512)))
  404.       (let ((size (tar-header-size tokens)))
  405.         (if (< size 0)
  406.         (error "%s has size %s - corrupted."
  407.                (tar-header-name tokens) size))
  408.             ;
  409.             ; This is just too slow.  Don't really need it anyway....
  410.             ;(check-tar-header-block-checksum
  411.             ;  hblock (checksum-tar-header-block hblock)
  412.             ;  (tar-header-name tokens))
  413.         
  414.         (setq result (cons (make-tar-desc pos tokens) result))
  415.         
  416.         (if (and (null (tar-header-link-type tokens))
  417.              (> size 0))
  418.         (setq pos
  419.               (+ pos 512 (ash (ash (1- size) -9) 9)) ; this works
  420.             ;(+ pos (+ size (- 512 (rem (1- size) 512)))) ; this doesn't
  421.               ))
  422.         ))))
  423.     (make-local-variable 'tar-parse-info)
  424.     (setq tar-parse-info (nreverse result)))
  425.   (message "parsing tar file...formatting...")
  426.   (save-excursion
  427.     (goto-char (point-min))
  428.     (let ((buffer-read-only nil))
  429.       (dolist (tar-desc tar-parse-info)
  430.     (insert
  431.      (summarize-tar-header-block (tar-desc-tokens tar-desc))
  432.      "\n"))
  433.       (make-local-variable 'tar-header-offset)
  434.       (setq tar-header-offset (point))
  435.       (narrow-to-region 1 tar-header-offset)
  436.       (set-buffer-modified-p nil)))
  437.   (message "parsing tar file...done."))
  438.  
  439.  
  440. (defvar tar-mode-map nil "*Local keymap for tar-mode listings.")
  441.  
  442. (if tar-mode-map
  443.     nil
  444.   (setq tar-mode-map (make-keymap))
  445.   (suppress-keymap tar-mode-map)
  446.   (define-key tar-mode-map " " 'tar-next-line)
  447.   (define-key tar-mode-map "c" 'tar-copy)
  448.   (define-key tar-mode-map "d" 'tar-flag-deleted)
  449.   (define-key tar-mode-map "\^D" 'tar-flag-deleted)
  450.   (define-key tar-mode-map "e" 'tar-extract)
  451.   (define-key tar-mode-map "f" 'tar-extract)
  452.   (define-key tar-mode-map "g" 'revert-buffer)
  453.   (define-key tar-mode-map "h" 'describe-mode)
  454.   (define-key tar-mode-map "n" 'tar-next-line)
  455.   (define-key tar-mode-map "\^N" 'tar-next-line)
  456.   (define-key tar-mode-map "o" 'tar-extract-other-window)
  457.   (define-key tar-mode-map "p" 'tar-previous-line)
  458.   (define-key tar-mode-map "\^P" 'tar-previous-line)
  459.   (define-key tar-mode-map "r" 'tar-rename-entry)
  460.   (define-key tar-mode-map "u" 'tar-unflag)
  461.   (define-key tar-mode-map "v" 'tar-view)
  462.   (define-key tar-mode-map "x" 'tar-expunge)
  463.   (define-key tar-mode-map 'backspace 'tar-unflag-backwards)
  464.   (define-key tar-mode-map 'delete 'tar-unflag-backwards)
  465.   (define-key tar-mode-map "E" 'tar-extract-other-window)
  466.   (define-key tar-mode-map "M" 'tar-chmod-entry)
  467.   (define-key tar-mode-map "G" 'tar-chgrp-entry)
  468.   (define-key tar-mode-map "O" 'tar-chown-entry)
  469.  
  470.   (cond ((and (featurep 'xemacs)
  471.           (not (featurep 'infodock)))
  472.      (define-key tar-mode-map 'button2 'tar-track-mouse-and-extract-file)
  473.      (define-key tar-mode-map 'button3 'tar-popup-menu)))
  474.   )
  475.  
  476.  
  477. ;; XEmacs menu mouse/support added by Heiko Muenkel
  478. ;; muenkel@tnt.uni-hannover.de
  479.  
  480. (autoload 'dired-mark-region "dired-xemacs-menu")
  481.  
  482. (defvar tar-menu
  483.   '("Tar Mode Commands"
  484.     ["Copy Subfile to Disk" tar-copy t]
  485.     ["Rename Subfile" tar-rename-entry t]
  486.     "----"
  487.     ["Delete Flaged Subfiles" tar-expunge t]
  488.     ["Flag Subfile for Deletion" tar-flag-deleted t]
  489.     ["Flag Subfiles in Region for Deletion"
  490.      (dired-mark-region '(tar-flag-deleted 1))
  491.      (mark)]
  492.     ["Unflag Subfile" tar-unflag t]
  493.     ["Unflag Subfiles in Region"
  494.      (dired-mark-region '(tar-flag-deleted 1 t))
  495.      (mark)]
  496.     "----"
  497.     ["Change Permissions of Subfile..." tar-chmod-entry t]
  498.     ["Change Group of Subfile..." tar-chgrp-entry t]
  499.     ["Change Owner of Subfile..." tar-chown-entry t]
  500.     "----"
  501.     ["Edit Subfile Other Window" tar-extract-other-window t]
  502.     ["Edit Subfile" tar-extract t]
  503.     ["View Subfile" tar-view t]
  504.     ))
  505.  
  506.  
  507. (defun tar-track-mouse-and-extract-file (event)
  508.   "Visit the tar-file-entry upon which the mouse is clicked."
  509.   (interactive "e")
  510.   (mouse-set-point event)
  511.   (tar-next-line 0)
  512.   (let (buffer)
  513.     (save-excursion
  514.       (tar-extract)
  515.       (setq buffer (current-buffer)))
  516.     (switch-to-buffer buffer)))
  517.  
  518. (defun tar-popup-menu (event)
  519.   "Display the tar-mode menu."
  520.   (interactive "@e")
  521.   (mouse-set-point event)
  522.   (tar-next-line 0)
  523.   (popup-menu tar-menu))
  524.  
  525.  
  526. ;; tar mode is suitable only for specially formatted data.
  527. (put 'tar-mode 'mode-class 'special)
  528. (put 'tar-subfile-mode 'mode-class 'special)
  529.  
  530. ;;;###autoload
  531. (defun tar-mode ()
  532.   "Major mode for viewing a tar file as a dired-like listing of its contents.
  533. You can move around using the usual cursor motion commands. 
  534. Letters no longer insert themselves.
  535. Type 'e' to pull a file out of the tar file and into its own buffer.
  536. Type 'c' to copy an entry from the tar file into another file on disk.
  537.  
  538. If you edit a sub-file of this archive (as with the 'e' command) and 
  539. save it with Control-X Control-S, the contents of that buffer will be 
  540. saved back into the tar-file buffer; in this way you can edit a file 
  541. inside of a tar archive without extracting it and re-archiving it.
  542.  
  543. See also: variables tar-update-datestamp and tar-anal-blocksize.
  544. \\{tar-mode-map}"
  545.   ;; this is not interactive because you shouldn't be turning this
  546.   ;; mode on and off.  You can corrupt things that way.
  547.   (make-local-variable 'tar-header-offset)
  548.   (make-local-variable 'tar-parse-info)
  549.   (make-local-variable 'require-final-newline)
  550.   (setq require-final-newline nil)    ; binary data, dude...
  551.   (make-local-variable 'revert-buffer-function)
  552.   (setq revert-buffer-function 'tar-mode-revert)
  553.   (setq major-mode 'tar-mode)
  554.   (setq mode-name "Tar")
  555.   (use-local-map tar-mode-map)
  556.   (auto-save-mode 0)
  557.   (widen)
  558.   (if (and (boundp 'tar-header-offset) tar-header-offset)
  559.       (narrow-to-region 1 tar-header-offset)
  560.     (tar-summarize-buffer))
  561.  
  562.   (cond ((string-match "XEmacs" emacs-version)
  563.      (require 'mode-motion)
  564.      (setq mode-motion-hook 'mode-motion-highlight-line)
  565.      (when (and (boundp 'current-menubar)
  566.             current-menubar
  567.             (not (assoc "Tar" current-menubar)))
  568.        (set-buffer-menubar (copy-sequence current-menubar))
  569.        (add-menu nil "Tar" (cdr tar-menu)))
  570.      ))
  571.   (run-hooks 'tar-mode-hook)
  572.   )
  573.  
  574. ;; buffer-local variables in subfile mode.
  575. ;;
  576. (defvar tar-subfile-mode nil)        ; whether the minor-mode is on
  577. (defvar superior-tar-buffer)        ; parent buffer
  578. (defvar superior-tar-descriptor)    ; header object of this file
  579. (defvar tar-subfile-buffer-id)        ; pretty name-string
  580. (defvar subfile-orig-mlbid)        ; orig mode-line-buffer-identification
  581.  
  582. (defun tar-subfile-mode (p)
  583.   "Minor mode for editing an element of a tar-file.
  584. This mode redefines ^X^S to save the current buffer back into its 
  585. associated tar-file buffer.  You must save that buffer to actually
  586. save your changes to disk."
  587.   (interactive "P")
  588.   (or (and (boundp 'superior-tar-buffer) superior-tar-buffer)
  589.       (error "This buffer is not an element of a tar file."))
  590.   (or (assq 'tar-subfile-mode minor-mode-alist)
  591.       (setq minor-mode-alist (append minor-mode-alist
  592.                      (list '(tar-subfile-mode " TarFile")))))
  593.   (make-local-variable 'tar-subfile-mode)
  594.   (setq tar-subfile-mode
  595.     (if (null p)
  596.         (not tar-subfile-mode)
  597.       (> (prefix-numeric-value p) 0)))
  598.   (cond (tar-subfile-mode
  599.      ;; copy the local keymap so that we don't accidentally
  600.      ;; alter a keymap like 'lisp-mode-map' which is shared
  601.      ;; by all buffers in that mode.
  602.      (let ((m (current-local-map)))
  603.        (if m (use-local-map (copy-keymap m))))
  604.      (local-set-key "\^X\^S" 'tar-subfile-save-buffer)
  605.      ;; turn off auto-save.
  606.      (auto-save-mode nil)
  607.      (setq buffer-auto-save-file-name nil)
  608.      (run-hooks 'tar-subfile-mode-hook))
  609.     (t
  610.      ;; remove the local binding for C-x C-s.
  611.      (local-unset-key "\^X\^S")
  612.      (if subfile-orig-mlbid
  613.          (set (make-local-variable 'mode-line-buffer-identification)
  614.           subfile-orig-mlbid))
  615.      (setq superior-tar-buffer nil
  616.            superior-tar-descriptor nil
  617.            subfile-orig-mlbid nil)
  618.      ))
  619.   )
  620.  
  621. (defun tar-subfile-after-write-file-hook ()
  622.   ;; if the buffer has a filename, then it is no longer associated with
  623.   ;; the tar file.  Turn off subfile mode.
  624.   (if (and buffer-file-name tar-subfile-mode)
  625.       (tar-subfile-mode -1)))
  626.  
  627. (defun tar-mode-revert (&optional no-autosave no-confirm)
  628.   "Revert this buffer and turn on tar mode again, to re-compute the
  629. directory listing."
  630.   (setq tar-header-offset nil)
  631.   (let ((revert-buffer-function nil))
  632.     (revert-buffer t no-confirm)
  633.     (widen))
  634.   (tar-mode))
  635.  
  636.  
  637. (defun tar-next-line (p)
  638.   (interactive "p")
  639.   (forward-line p)
  640.   (if (eobp) nil (forward-char (if tar-can-print-dates 48 36))))
  641.  
  642. (defun tar-previous-line (p)
  643.   (interactive "p")
  644.   (tar-next-line (- p)))
  645.  
  646. (defun tar-current-descriptor (&optional noerror)
  647.   "Returns the tar-descriptor of the current line, or signals an error."
  648.   ;; I wish lines had plists, like in ZMACS...
  649.   (or (nth (count-lines (point-min)
  650.             (save-excursion (beginning-of-line) (point)))
  651.        tar-parse-info)
  652.       (if noerror
  653.       nil
  654.     (error "This line does not describe a tar-file entry."))))
  655.  
  656.  
  657. (defun tar-extract (&optional other-window-p)
  658.   "*In tar-mode, extract this entry of the tar file into its own buffer."
  659.   (interactive)
  660.   (let* ((view-p (eq other-window-p 'view))
  661.      (descriptor (tar-current-descriptor))
  662.      (tokens (tar-desc-tokens descriptor))
  663.      (name (tar-header-name tokens))
  664.      (size (tar-header-size tokens))
  665.      (link-p (tar-header-link-type tokens))
  666.      (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
  667.      (end (+ start size)))
  668.     (if link-p
  669.     (error "This is a %s, not a real file."
  670.            (cond ((eq link-p 5) "directory")
  671.              ((eq link-p 20) "tar directory header")
  672.              ((eq link-p 29) "multivolume-continuation")
  673.              ((eq link-p 35) "sparse entry")
  674.              ((eq link-p 38) "volume header")
  675.              (t "link"))))
  676.     (if (zerop size) (error "This is a zero-length file."))
  677.     (let* ((tar-buffer (current-buffer))
  678.        (bufname (file-name-nondirectory name))
  679.        (bufid (concat        ;" (" name " in "
  680.            " (in "
  681.            (file-name-nondirectory (buffer-file-name))
  682.            ")"))
  683.        (read-only-p (or buffer-read-only view-p))
  684.        (buffer nil)
  685.        (buffers (buffer-list))
  686.        (just-created nil))
  687.       ;; find a buffer visiting this subfile from this tar file.
  688.       (while (and buffers (not buffer))
  689.     (set-buffer (car buffers))
  690.     (if (and (null (buffer-file-name (car buffers)))
  691.          (boundp 'superior-tar-descriptor)
  692.          (eq superior-tar-descriptor descriptor))
  693.         (setq buffer (car buffers))
  694.       (setq buffers (cdr buffers))))
  695.       (set-buffer tar-buffer)
  696.       (if buffer
  697.       nil
  698.     (setq buffer (generate-new-buffer bufname))
  699.     (setq just-created t)
  700.     (unwind-protect
  701.         (progn
  702.           (widen)
  703.           (save-excursion
  704.         (set-buffer buffer)
  705.         (insert-buffer-substring tar-buffer start end)
  706.         (goto-char 0)
  707.         (let ((lock-directory nil)) ; disable locking
  708.           (set-visited-file-name name) ; give it a name to decide mode.
  709.           ;;          (normal-mode)  ; pick a mode.
  710.           ;;          (after-find-file nil nil)  ; pick a mode; works with crypt.el
  711.           ;; Ok, instead of running after-find-file, just invoke the
  712.           ;; find-file-hooks instead.  This does everything we want
  713.           ;; from after-find-file, without losing when visiting .tar
  714.           ;; files via ange-ftp: doesn't probe the ftp site for the
  715.           ;; name of the subfile.
  716.           (normal-mode t)
  717.           (run-hooks 'find-file-hooks)
  718.           (set-visited-file-name nil) ; nuke the name - not meaningful.
  719.           )
  720.         (make-local-variable 'superior-tar-buffer)
  721.         (make-local-variable 'superior-tar-descriptor)
  722.         (make-local-variable 'mode-line-buffer-identification)
  723.         (make-local-variable 'tar-subfile-buffer-id)
  724.         (make-local-variable 'subfile-orig-mlbid)
  725.         (setq superior-tar-buffer tar-buffer)
  726.         (setq superior-tar-descriptor descriptor)
  727.         (setq tar-subfile-buffer-id bufid)
  728.         (setq subfile-orig-mlbid mode-line-buffer-identification)
  729.         (cond ((stringp mode-line-buffer-identification)
  730.                (setq mode-line-buffer-identification
  731.                  (list mode-line-buffer-identification))))
  732.         (let ((ms (car mode-line-buffer-identification))
  733.               n)
  734.           (cond ((and (stringp ms)
  735.                   (string-match "%\\([0-9]+\\)b\\'" ms))
  736.              (setq mode-line-buffer-identification
  737.                    (cons
  738.                 (concat (substring ms 0
  739.                            (1- (match-beginning 1)))
  740.                     (substring ms (1+ (match-end 1))))
  741.                 (cons
  742.                  (list (car (read-from-string
  743.                          (substring ms (match-beginning 1)
  744.                             (match-end 1))))
  745.                        (concat "%b" tar-subfile-buffer-id))
  746.                  (cdr mode-line-buffer-identification)))))
  747.             (t
  748.              (setq mode-line-buffer-identification
  749.                    (list "Emacs: "
  750.                      (list 17
  751.                        (concat "%b"
  752.                            tar-subfile-buffer-id)))))))
  753.         (tar-subfile-mode 1)
  754.         
  755.         (setq buffer-read-only read-only-p)
  756.         (set-buffer-modified-p nil))
  757.           (set-buffer tar-buffer))
  758.       (narrow-to-region 1 tar-header-offset)))
  759.       (if view-p
  760.       (progn
  761.         (view-buffer-other-window buffer)
  762.         (save-excursion
  763.           (set-buffer buffer)
  764.           ;; for view-less.el; view.el can't do this.
  765.           (set (make-local-variable 'view-kill-on-exit) t)))
  766.     (if other-window-p
  767.         (switch-to-buffer-other-window buffer)
  768.       (switch-to-buffer buffer))))))
  769.  
  770.  
  771. (defun tar-extract-other-window ()
  772.   "*In tar-mode, extract this entry of the tar file into its own buffer."
  773.   (interactive)
  774.   (tar-extract t))
  775.  
  776. (defun tar-view ()
  777.   "*In tar-mode, view the tar file entry on this line."
  778.   (interactive)
  779.   (tar-extract 'view))
  780.  
  781.  
  782. (defun tar-read-file-name (&optional prompt)
  783.   "Calls read-file-name, with the default being the file of the current
  784. tar-file descriptor."
  785.   (or prompt (setq prompt "Copy to: "))
  786.   (let* ((default-file (expand-file-name
  787.             (tar-header-name (tar-desc-tokens
  788.                       (tar-current-descriptor)))))
  789.      (target (expand-file-name
  790.           (read-file-name prompt
  791.                   (file-name-directory default-file)
  792.                   default-file nil))))
  793.     (if (or (string= "" (file-name-nondirectory target))
  794.         (file-directory-p target))
  795.     (setq target (concat (if (string-match "/$" target)
  796.                  (substring target 0 (1- (match-end 0)))
  797.                    target)
  798.                  "/"
  799.                  (file-name-nondirectory default-file))))
  800.     target))
  801.  
  802.  
  803. (defun tar-copy (&optional to-file)
  804.   "*In tar-mode, extract this entry of the tar file into a file on disk.
  805. If TO-FILE is not supplied, it is prompted for, defaulting to the name of
  806. the current tar-entry."
  807.   (interactive (list (tar-read-file-name)))
  808.   (let* ((descriptor (tar-current-descriptor))
  809.      (tokens (tar-desc-tokens descriptor))
  810.      (name (tar-header-name tokens))
  811.      (size (tar-header-size tokens))
  812.      (link-p (tar-header-link-type tokens))
  813.      (start (+ (tar-desc-data-start descriptor) tar-header-offset -1))
  814.      (end (+ start size)))
  815.     (if link-p (error "This is a link, not a real file."))
  816.     (if (zerop size) (error "This is a zero-length file."))
  817.     (let* ((tar-buffer (current-buffer))
  818.        buffer)
  819.       (unwind-protect
  820.       (progn
  821.         (setq buffer (generate-new-buffer "*tar-copy-tmp*"))
  822.         (widen)
  823.         (save-excursion
  824.           (set-buffer buffer)
  825.           (insert-buffer-substring tar-buffer start end)
  826.           (set-buffer-modified-p nil) ; in case we abort
  827.           (write-file to-file)
  828.           (message "Copied tar entry %s to %s" name to-file)
  829.           (set-buffer tar-buffer)))
  830.     (narrow-to-region 1 tar-header-offset)
  831.     (if buffer (kill-buffer buffer)))
  832.       )))
  833.  
  834.  
  835. (defun tar-flag-deleted (p &optional unflag)
  836.   "*In tar mode, mark this sub-file to be deleted from the tar file.
  837. With a prefix argument, mark that many files."
  838.   (interactive "p")
  839.   (beginning-of-line)
  840.   (dotimes (i (if (< p 0) (- p) p))
  841.     (if (tar-current-descriptor unflag) ; barf if we're not on an entry-line.
  842.     (progn
  843.       (delete-char 1)
  844.       (insert (if unflag " " "D"))))
  845.     (forward-line (if (< p 0) -1 1)))
  846.   (if (eobp) nil (forward-char 36)))
  847.  
  848. (defun tar-unflag (p)
  849.   "*In tar mode, un-mark this sub-file if it is marked to be deleted.
  850. With a prefix argument, un-mark that many files forward."
  851.   (interactive "p")
  852.   (tar-flag-deleted p t))
  853.  
  854. (defun tar-unflag-backwards (p)
  855.   "*In tar mode, un-mark this sub-file if it is marked to be deleted.
  856. With a prefix argument, un-mark that many files backward."
  857.   (interactive "p")
  858.   (tar-flag-deleted (- p) t))
  859.  
  860.  
  861. (defun tar-expunge-internal ()
  862.   "Expunge the tar-entry specified by the current line."
  863.   (let* ((descriptor (tar-current-descriptor))
  864.      (tokens (tar-desc-tokens descriptor))
  865.      (line (tar-desc-data-start descriptor))
  866.      (name (tar-header-name tokens))
  867.      (size (tar-header-size tokens))
  868.      (link-p (tar-header-link-type tokens))
  869.      (start (tar-desc-data-start descriptor))
  870.      (following-descs (cdr (memq descriptor tar-parse-info))))
  871.     (if link-p (setq size 0))        ; size lies for hard-links.
  872.     ;;
  873.     ;; delete the current line...
  874.     (beginning-of-line)
  875.     (let ((line-start (point)))
  876.       (end-of-line) (forward-char)
  877.       (let ((line-len (- (point) line-start)))
  878.     (delete-region line-start (point))
  879.     ;;
  880.     ;; decrement the header-pointer to be in synch...
  881.     (setq tar-header-offset (- tar-header-offset line-len))))
  882.     ;;
  883.     ;; delete the data pointer...
  884.     (setq tar-parse-info (delq descriptor tar-parse-info))
  885.     ;;
  886.     ;; delete the data from inside the file...
  887.     (widen)
  888.     (let* ((data-start (+ start tar-header-offset -513))
  889.        (data-end (+ data-start 512 (ash (ash (+ size 511) -9) 9))))
  890.       (delete-region data-start data-end)
  891.       ;;
  892.       ;; and finally, decrement the start-pointers of all following
  893.       ;; entries in the archive.  This is a pig when deleting a bunch
  894.       ;; of files at once - we could optimize this to only do the
  895.       ;; iteration over the files that remain, or only iterate up to
  896.       ;; the next file to be deleted.
  897.       (let ((data-length (- data-end data-start)))
  898.     (dolist (desc following-descs)
  899.       (setf (tar-desc-data-start desc)
  900.         (- (tar-desc-data-start desc) data-length))))
  901.       ))
  902.   (narrow-to-region 1 tar-header-offset))
  903.  
  904.  
  905. (defun tar-expunge (&optional noconfirm)
  906.   "*In tar-mode, delete all the archived files flagged for deletion.
  907. This does not modify the disk image; you must save the tar file itself
  908. for this to be permanent."
  909.   (interactive)
  910.   (if (or noconfirm
  911.       (y-or-n-p "expunge files marked for deletion? "))
  912.       (let ((n 0))
  913.     (save-excursion
  914.       (goto-char 0)
  915.       (while (not (eobp))
  916.         (if (looking-at "D")
  917.         (progn (tar-expunge-internal)
  918.                (setq n (1+ n)))
  919.           (forward-line 1)))
  920.       ;; after doing the deletions, add any padding that may be necessary.
  921.       (tar-pad-to-blocksize)
  922.       (narrow-to-region 1 tar-header-offset)
  923.       )
  924.     (if (zerop n)
  925.         (message "nothing to expunge.")
  926.       (message "%s expunged.  Be sure to save this buffer." n)))))
  927.  
  928.  
  929. (defun tar-clear-modification-flags ()
  930.   "remove the stars at the beginning of each line."
  931.   (save-excursion
  932.     (goto-char 0)
  933.     (while (< (point) tar-header-offset)
  934.       (if (looking-at "*")
  935.       (progn (delete-char 1) (insert " ")))
  936.       (forward-line 1))))
  937.  
  938.  
  939. (defun tar-chown-entry (new-uid)
  940.   "*Change the user-id associated with this entry in the tar file.
  941. If this tar file was written by GNU tar, then you will be able to edit
  942. the user id as a string; otherwise, you must edit it as a number.
  943. You can force editing as a number by calling this with a prefix arg.
  944. This does not modify the disk image; you must save the tar file itself
  945. for this to be permanent."
  946.   (interactive (list
  947.         (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
  948.           (if (or current-prefix-arg
  949.               (not (tar-header-magic tokens)))
  950.               (let (n)
  951.             (while (not (numberp (setq n (read-minibuffer
  952.                               "New UID number: "
  953.                               (format "%s" (tar-header-uid tokens)))))))
  954.             n)
  955.             (read-string "New UID string: " (tar-header-uname tokens))))))
  956.   (cond ((stringp new-uid)
  957.      (setf (tar-header-uname (tar-desc-tokens (tar-current-descriptor)))
  958.            new-uid)
  959.      (tar-alter-one-field tar-uname-offset (concat new-uid "\000")))
  960.     (t
  961.      (setf (tar-header-uid (tar-desc-tokens (tar-current-descriptor)))
  962.            new-uid)
  963.      (tar-alter-one-field tar-uid-offset
  964.                   (concat (substring (format "%6o" new-uid) 0 6) "\000 ")))))
  965.  
  966.  
  967. (defun tar-chgrp-entry (new-gid)
  968.   "*Change the group-id associated with this entry in the tar file.
  969. If this tar file was written by GNU tar, then you will be able to edit
  970. the group id as a string; otherwise, you must edit it as a number.
  971. You can force editing as a number by calling this with a prefix arg.
  972. This does not modify the disk image; you must save the tar file itself
  973. for this to be permanent."
  974.   (interactive (list
  975.         (let ((tokens (tar-desc-tokens (tar-current-descriptor))))
  976.           (if (or current-prefix-arg
  977.               (not (tar-header-magic tokens)))
  978.               (let (n)
  979.             (while (not (numberp (setq n (read-minibuffer
  980.                               "New GID number: "
  981.                               (format "%s" (tar-header-gid tokens)))))))
  982.             n)
  983.             (read-string "New GID string: " (tar-header-gname tokens))))))
  984.   (cond ((stringp new-gid)
  985.      (setf (tar-header-gname (tar-desc-tokens (tar-current-descriptor)))
  986.            new-gid)
  987.      (tar-alter-one-field tar-gname-offset
  988.                   (concat new-gid "\000")))
  989.     (t
  990.      (setf (tar-header-gid (tar-desc-tokens (tar-current-descriptor)))
  991.            new-gid)
  992.      (tar-alter-one-field tar-gid-offset
  993.                   (concat (substring (format "%6o" new-gid) 0 6) "\000 ")))))
  994.  
  995. (defun tar-rename-entry (new-name)
  996.   "*Change the name associated with this entry in the tar file.
  997. This does not modify the disk image; you must save the tar file itself
  998. for this to be permanent."
  999.   (interactive
  1000.    (list (read-string "New name: "
  1001.               (tar-header-name (tar-desc-tokens (tar-current-descriptor))))))
  1002.   (if (string= "" new-name) (error "zero length name."))
  1003.   (if (> (length new-name) 98) (error "name too long."))
  1004.   (setf (tar-header-name (tar-desc-tokens (tar-current-descriptor)))
  1005.     new-name)
  1006.   (tar-alter-one-field 0
  1007.                (substring (concat new-name (make-string 99 0)) 0 99)))
  1008.  
  1009.  
  1010. (defun tar-chmod-entry (new-mode)
  1011.   "*Change the protection bits associated with this entry in the tar file.
  1012. This does not modify the disk image; you must save the tar file itself
  1013. for this to be permanent."
  1014.   (interactive (list (tar-parse-octal-integer-safe
  1015.               (read-string "New protection (octal): "))))
  1016.   (setf (tar-header-mode (tar-desc-tokens (tar-current-descriptor)))
  1017.     new-mode)
  1018.   (tar-alter-one-field tar-mode-offset
  1019.                (concat (substring (format "%6o" new-mode) 0 6) "\000 ")))
  1020.  
  1021.  
  1022. (defun tar-alter-one-field (data-position new-data-string)
  1023.   (let* ((descriptor (tar-current-descriptor))
  1024.      (tokens (tar-desc-tokens descriptor)))
  1025.     (unwind-protect
  1026.     (save-excursion
  1027.       ;;
  1028.       ;; update the header-line.
  1029.       (beginning-of-line)
  1030.       (let ((p (point)))
  1031.         (forward-line 1)
  1032.         (delete-region p (point))
  1033.         (insert (summarize-tar-header-block tokens) "\n")
  1034.         (setq tar-header-offset (point-max)))
  1035.       
  1036.       (widen)
  1037.       (let* ((start (+ (tar-desc-data-start descriptor) tar-header-offset -513)))
  1038.         ;;
  1039.         ;; delete the old field and insert a new one.
  1040.         (goto-char (+ start data-position))
  1041.         (delete-region (point) (+ (point) (length new-data-string))) ; <--
  1042.         (insert new-data-string)    ; <--
  1043.         ;;
  1044.         ;; compute a new checksum and insert it.
  1045.         (let ((chk (checksum-tar-header-block
  1046.             (buffer-substring start (+ start 512)))))
  1047.           (goto-char (+ start tar-chk-offset))
  1048.           (delete-region (point) (+ (point) 8))
  1049.           (insert (format "%6o" chk))
  1050.           (insert 0)
  1051.           (insert ? )
  1052.           (setf (tar-header-checksum tokens) chk)
  1053.           ;;
  1054.           ;; ok, make sure we didn't botch it.
  1055.           (check-tar-header-block-checksum
  1056.            (buffer-substring start (+ start 512))
  1057.            chk (tar-header-name tokens))
  1058.           )))
  1059.       (narrow-to-region 1 tar-header-offset))))
  1060.  
  1061.  
  1062. (defun tar-subfile-save-buffer ()
  1063.   "In tar subfile mode, write this buffer back into its parent tar-file buffer.
  1064. This doesn't write anything to disk - you must save the parent tar-file buffer
  1065. to make your changes permanent."
  1066.   (interactive)
  1067.   (cond (buffer-file-name
  1068.      ;; tar-subfile buffers should have nil as buffer-file-name.  If they
  1069.      ;; ever gain a buffer-file-name, that means they have been written to
  1070.      ;; a real disk file, as with ^X^W.  If this happens, behave just like
  1071.      ;; `save-buffer.'
  1072.      (call-interactively 'save-buffer))
  1073.     (t
  1074.      (tar-subfile-save-buffer-internal))))
  1075.  
  1076. (defun tar-subfile-save-buffer-internal ()
  1077.   (if (not (and (boundp 'superior-tar-buffer) superior-tar-buffer))
  1078.       (error "this buffer has no superior tar file buffer."))
  1079.   (or (buffer-name superior-tar-buffer)
  1080.       (error "the superior tar file's buffer has been killed."))
  1081.   (if (not (and (boundp 'superior-tar-descriptor) superior-tar-descriptor))
  1082.       (error "this buffer doesn't have an index into its superior tar file!"))
  1083.  
  1084.   ;; Notice when crypt.el has uncompressed while reading the subfile, and
  1085.   ;; signal an error if the user tries to save back into the parent file
  1086.   ;; (because it won't work - the .Z subfile it writes won't really be
  1087.   ;; compressed.)
  1088.   ;;
  1089.                     ;  ;; These are for the old crypt.el
  1090.                     ;  (if (and (boundp 'buffer-save-encrypted) buffer-save-encrypted)
  1091.                     ;      (error "Don't know how to encrypt back into a tar file."))
  1092.                     ;  (if (and (boundp 'buffer-save-compacted) buffer-save-compacted)
  1093.                     ;      (error "Don't know how to compact back into a tar file."))
  1094.                     ;  (if (and (boundp 'buffer-save-compressed) buffer-save-compressed)
  1095.                     ;      (error "Don't know how to compress back into a tar file."))
  1096.                     ;  (if (and (boundp 'buffer-save-gzipped) buffer-save-gzipped)
  1097.                     ;      (error "Don't know how to gzip back into a tar file."))
  1098.  
  1099.   ;; These are for the new crypt++.el
  1100.   (if (and (boundp 'crypt-buffer-save-encrypted) crypt-buffer-save-encrypted)
  1101.       (error "Don't know how to encrypt back into a tar file."))
  1102.   (if (and (boundp 'crypt-buffer-save-compact) crypt-buffer-save-compact)
  1103.       (error "Don't know how to compact back into a tar file."))
  1104.   (if (and (boundp 'crypt-buffer-save-compress) crypt-buffer-save-compress)
  1105.       (error "Don't know how to compress back into a tar file."))
  1106.   (if (and (boundp 'crypt-buffer-save-gzip) crypt-buffer-save-gzip)
  1107.       (error "Don't know how to gzip back into a tar file."))
  1108.   (if (and (boundp 'crypt-buffer-save-freeze) crypt-buffer-save-freeze)
  1109.       (error "Don't know how to freeze back into a tar file."))
  1110.  
  1111.   (save-excursion
  1112.     (let ((subfile (current-buffer))
  1113.       (subfile-size (buffer-size))
  1114.       (descriptor superior-tar-descriptor))
  1115.       (set-buffer superior-tar-buffer)
  1116.       (let* ((tokens (tar-desc-tokens descriptor))
  1117.          (start (tar-desc-data-start descriptor))
  1118.          (name (tar-header-name tokens))
  1119.          (size (tar-header-size tokens))
  1120.          (size-pad (ash (ash (+ size 511) -9) 9))
  1121.          (head (memq descriptor tar-parse-info))
  1122.          (following-descs (cdr head)))
  1123.     (if (not head)
  1124.         (error "Can't find this tar file entry in its parent tar file!"))
  1125.     (unwind-protect
  1126.         (save-excursion
  1127.           (widen)
  1128.           ;; delete the old data...
  1129.           (let* ((data-start (+ start tar-header-offset -1))
  1130.              (data-end (+ data-start (ash (ash (+ size 511) -9) 9))))
  1131.         (delete-region data-start data-end)
  1132.         ;; insert the new data...
  1133.         (goto-char data-start)
  1134.         (insert-buffer subfile)
  1135.         ;;
  1136.         ;; pad the new data out to a multiple of 512...
  1137.         (let ((subfile-size-pad (ash (ash (+ subfile-size 511) -9) 9)))
  1138.           (goto-char (+ data-start subfile-size))
  1139.           (insert (make-string (- subfile-size-pad subfile-size) 0))
  1140.           ;;
  1141.           ;; update the data pointer of this and all following files...
  1142.           (setf (tar-header-size tokens) subfile-size)
  1143.           (let ((difference (- subfile-size-pad size-pad)))
  1144.             (dolist (desc following-descs)
  1145.               (setf (tar-desc-data-start desc)
  1146.                 (+ (tar-desc-data-start desc) difference))))
  1147.           ;;
  1148.           ;; Update the size field in the header block.
  1149.           (let ((header-start (- data-start 512)))
  1150.             (goto-char (+ header-start tar-size-offset))
  1151.             (delete-region (point) (+ (point) 12))
  1152.             (insert (format "%11o" subfile-size))
  1153.             (insert ? )
  1154.             ;;
  1155.             ;; Maybe update the datestamp.
  1156.             (if (not tar-update-datestamp)
  1157.             nil
  1158.               (goto-char (+ header-start tar-time-offset))
  1159.               (delete-region (point) (+ (point) 12))
  1160.               (let (now top bot)
  1161.             (cond ((fboundp 'current-time)
  1162.                    (setq now (current-time))
  1163.                    (setcdr now (car (cdr now))))
  1164.                     ;            ((fboundp 'current-time-seconds)
  1165.                     ;             (setq now (current-time-seconds)))
  1166.                   )
  1167.             (setq top (car now)
  1168.                   bot (cdr now))
  1169.             (cond
  1170.              (now
  1171.               (setf (tar-header-date tokens) now)
  1172.               ;; hair to print two 16-bit numbers as one octal number.
  1173.               (setq bot (logior (ash (logand top 3) 16) bot))
  1174.               (setq top (ash top -2))
  1175.               (insert (format "%5o" top))
  1176.               (insert (format "%06o " bot)))
  1177.              (t
  1178.               ;; otherwise, set it to the epoch.
  1179.               (insert (format "%11o " 0))
  1180.               (setf (tar-header-date tokens) (cons 0 0))
  1181.               ))))
  1182.             ;;
  1183.             ;; compute a new checksum and insert it.
  1184.             (let ((chk (checksum-tar-header-block
  1185.                 (buffer-substring header-start data-start))))
  1186.               (goto-char (+ header-start tar-chk-offset))
  1187.               (delete-region (point) (+ (point) 8))
  1188.               (insert (format "%6o" chk))
  1189.               (insert 0)
  1190.               (insert ? )
  1191.               (setf (tar-header-checksum tokens) chk)))
  1192.           ;;
  1193.           ;; alter the descriptor-line...
  1194.           ;;
  1195.           (let ((position (- (length tar-parse-info) (length head))))
  1196.             (goto-char 1)
  1197.             (next-line position)
  1198.             (beginning-of-line)
  1199.             (let ((p (point))
  1200.               (m (set-marker (make-marker) tar-header-offset)))
  1201.               (forward-line 1)
  1202.               (delete-region p (point))
  1203.               (insert-before-markers (summarize-tar-header-block tokens t) "\n")
  1204.               (setq tar-header-offset (marker-position m)))
  1205.             )))
  1206.           ;; after doing the insertion, add any final padding that may be necessary.
  1207.           (tar-pad-to-blocksize))
  1208.       (narrow-to-region 1 tar-header-offset)))
  1209.       (set-buffer-modified-p t)        ; mark the tar file as modified
  1210.       (set-buffer subfile)
  1211.       (set-buffer-modified-p nil)    ; mark the tar subfile as unmodified
  1212.       (message "saved into tar-buffer \"%s\" - remember to save that buffer!"
  1213.            (buffer-name superior-tar-buffer))
  1214.       )))
  1215.  
  1216.  
  1217. (defun tar-pad-to-blocksize ()
  1218.   "If we are being anal about tar file blocksizes, fix up the current buffer.
  1219. Leaves the region wide."
  1220.   (if (null tar-anal-blocksize)
  1221.       nil
  1222.     (widen)
  1223.     (let* ((last-desc (nth (1- (length tar-parse-info)) tar-parse-info))
  1224.        (start (tar-desc-data-start last-desc))
  1225.        (tokens (tar-desc-tokens last-desc))
  1226.        (link-p (tar-header-link-type tokens))
  1227.        (size (if link-p 0 (tar-header-size tokens)))
  1228.        (data-end (+ start size))
  1229.        (bbytes (ash tar-anal-blocksize 9))
  1230.        (pad-to (+ bbytes (* bbytes (/ (1- data-end) bbytes))))
  1231.        (buffer-read-only nil)    ; ##
  1232.        )
  1233.       ;; If the padding after the last data is too long, delete some;
  1234.       ;; else insert some until we are padded out to the right number of blocks.
  1235.       ;;
  1236.       (goto-char (+ (or tar-header-offset 0) data-end))
  1237.       (if (> (1+ (buffer-size)) (+ (or tar-header-offset 0) pad-to))
  1238.       (delete-region (+ (or tar-header-offset 0) pad-to) (1+ (buffer-size)))
  1239.     (insert (make-string (- (+ (or tar-header-offset 0) pad-to)
  1240.                 (1+ (buffer-size)))
  1241.                  0)))
  1242.       )))
  1243.  
  1244.  
  1245. (defun maybe-write-tar-file ()
  1246.   "Used as a write-file-hook to write tar-files out correctly."
  1247.   ;;
  1248.   ;; If the current buffer is in tar-mode and has its header-offset set,
  1249.   ;; remove the header from the file, call the remaining write-file hooks,
  1250.   ;; and then write out the buffer (if and only if one of the write-file
  1251.   ;; hooks didn't write it already).  Then put the header back on the
  1252.   ;; buffer.  Many thanks to Piet van Oostrum for this code, which causes
  1253.   ;; correct interaction with crypt.el (and probably anything like it.)
  1254.   ;;
  1255.   ;; Kludge: in XEmacs Emacs, write-file-hooks is bound to nil before the
  1256.   ;; write-file-hooks are run, to prevent them from being run recursively
  1257.   ;; (this is more of a danger in v19-vintage emacses, which have both
  1258.   ;; write-file-hooks and write-contents-hooks.)  So, we need to reference
  1259.   ;; an internal variable of basic-save-buffer to get the list of hooks
  1260.   ;; remaining to be run.
  1261.   ;;
  1262.   (and (eq major-mode 'tar-mode)
  1263.        (and (boundp 'tar-header-offset) tar-header-offset)
  1264.        (let* ((hooks (cond ((string-match "XEmacs" emacs-version)
  1265.                 ;; Internal to basic-save-buffer in XEmacs.
  1266.                 (symbol-value 'hooks))
  1267.                ((string-lessp "19" emacs-version)
  1268.                 ;; I think this is what we need to do in fsfmacs.
  1269.                 (append write-contents-hooks write-file-hooks))
  1270.                (t
  1271.                 write-file-hooks)))
  1272.           (remaining-hooks (cdr (memq 'maybe-write-tar-file hooks)))
  1273.           header-string
  1274.           done)
  1275.      (save-excursion
  1276.        (save-restriction
  1277.          (widen)
  1278.          (tar-clear-modification-flags)
  1279.          (setq header-string (buffer-substring 1 tar-header-offset))
  1280.          (delete-region 1 tar-header-offset)
  1281.          (unwind-protect
  1282.          (progn
  1283.            (while (and remaining-hooks
  1284.                    (not (setq done (funcall (car remaining-hooks)))))
  1285.              (setq remaining-hooks (cdr remaining-hooks)))
  1286.            (cond ((not done)
  1287.               (write-region 1 (1+ (buffer-size))
  1288.                     buffer-file-name nil t)
  1289.               (setq done t))))
  1290.            (goto-char 1)
  1291.            (insert header-string)
  1292.            (set-buffer-modified-p nil))))
  1293.      done)))
  1294.  
  1295.  
  1296. ;;; Patch it in.
  1297.  
  1298. (defvar tar-regexp "\\.\\(tar\\|tgz\\)$"
  1299.   "The regular expression used to identify tar file names.")
  1300.  
  1301. (setq auto-mode-alist
  1302.       (cons (cons tar-regexp 'tar-mode) auto-mode-alist))
  1303.  
  1304. ;; Note: the tar write-file-hook should go on the list *before* any other
  1305. ;; hooks which might write the file.  Since things like crypt-mode add things
  1306. ;; to the end of the write-file-hooks, this will normally be the case.
  1307.  
  1308.                     ;(or (boundp 'write-file-hooks) (setq write-file-hooks nil))
  1309.                     ;(or (listp write-file-hooks)
  1310.                     ;    (setq write-file-hooks (list write-file-hooks)))
  1311.                     ;(or (memq 'maybe-write-tar-file write-file-hooks)
  1312.                     ;    (setq write-file-hooks
  1313.                     ;      (cons 'maybe-write-tar-file write-file-hooks)))
  1314.  
  1315. (add-hook 'write-file-hooks 'maybe-write-tar-file) ; ####write-contents-hooks??
  1316. (cond ((boundp 'after-save-hook)
  1317.        (add-hook 'after-save-hook 'tar-subfile-after-write-file-hook))
  1318.       ((boundp 'after-write-file-hooks)
  1319.        (add-hook 'after-write-file-hooks 'tar-subfile-after-write-file-hook))
  1320.       (t (error "neither after-save-hook nor after-write-file-hooks?")))
  1321.  
  1322.  
  1323. ;;; This is a hack.  For files ending in .tar, we want -*- lines to be
  1324. ;;; completely ignored - if there is one, it applies to the first file
  1325. ;;; in the archive, and not the archive itself!  Similarly for local
  1326. ;;; variables specifications in the last file of the archive.
  1327.  
  1328. (defun tar-normal-mode (&optional find-file)
  1329.   "Choose the major mode for this buffer automatically.
  1330. Also sets up any specified local variables of the file.
  1331. Uses the visited file name, the -*- line, and the local variables spec.
  1332.  
  1333. This function is called automatically from `find-file'.  In that case,
  1334. if `inhibit-local-variables' is non-`nil' we require confirmation before
  1335. processing a local variables spec.  If you run `normal-mode' explicitly,
  1336. confirmation is never required.
  1337.  
  1338. Note that this version of this function has been hacked to interact
  1339. correctly with tar files - when visiting a file which matches
  1340. 'tar-regexp', the -*- line and local-variables are not examined,
  1341. as they would apply to a file within the archive rather than the archive
  1342. itself."
  1343.   (interactive)
  1344.   (if (and buffer-file-name
  1345.        (string-match tar-regexp buffer-file-name))
  1346.       (tar-mode)
  1347.     (tar-real-normal-mode find-file)))
  1348.  
  1349. ;; We have to shadow this as well to get along with crypt.el.
  1350. ;; Shadowing this alone isn't enough, though; we need to shadow 
  1351. ;; tar-normal-mode in order to inhibit the local variables of the
  1352. ;; last file in the tar archive.
  1353. ;;
  1354. (defun tar-set-auto-mode ()
  1355.   "Select major mode appropriate for current buffer.
  1356. May base decision on visited file name (See variable  auto-mode-list)
  1357. or on buffer contents (-*- line or local variables spec), but does not look
  1358. for the \"mode:\" local variable.  For that, use  hack-local-variables.
  1359.  
  1360. Note that this version of this function has been hacked to interact
  1361. correctly with tar files - when visiting a file which matches
  1362. 'tar-regexp', the -*- line and local-variables are not examined,
  1363. as they would apply to a file within the archive rather than the archive
  1364. itself."
  1365.   (interactive)
  1366.   (if (and buffer-file-name
  1367.        (string-match tar-regexp buffer-file-name))
  1368.       (tar-mode)
  1369.     (tar-real-set-auto-mode)))
  1370.  
  1371. (if (not (fboundp 'tar-real-normal-mode))
  1372.     (fset 'tar-real-normal-mode (symbol-function 'normal-mode)))
  1373. (fset 'normal-mode 'tar-normal-mode)
  1374.  
  1375. (if (not (fboundp 'tar-real-set-auto-mode))
  1376.     (fset 'tar-real-set-auto-mode (symbol-function 'set-auto-mode)))
  1377. (fset 'set-auto-mode 'tar-set-auto-mode)
  1378.  
  1379. (provide 'tar-mode)
  1380.  
  1381. ;;; tar-mode.el ends here
  1382.